﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using BLL;
namespace Stationery.UC
{
    public partial class UC_Advertise_Update : System.Web.UI.UserControl
    {
        private Advertise advertise = new Advertise();
        private BLL_Advertise bll_Advertise = new BLL_Advertise();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["status"] == "edit")
            {
                Int64 intID = Convert.ToInt64(Request.Params["id_advertise"].ToString());
                advertise = bll_Advertise.Lst_Advertise(bll_Advertise.Get_Advertise(intID))[0];
                ASPxTextBoxName.Text = advertise.NAME;
                ASPxTextBoxWebLink.Text = advertise.WEB_LINK;
                //ImageAdvertise.ImageUrl=advertise.IMAGE_DIRECTORY;
            }
        }

        protected void ASPxButtonSave_Click(object sender, EventArgs e)
        {
            //luu
            string strStatus = "";
            string strFileName = "";
            Int32 intID;
            if (Request.Params["status"] != null)
            {
                strStatus = Request.Params["status"].ToString();
                if (Request.Params["status"] == "edit")
                {
                    intID = Convert.ToInt32(Request.Params["id_advertise"].ToString());
                    advertise.ID = intID;
                }
            }
            advertise.NAME = ASPxTextBoxName.Text;
            try
            {
                if (ASPxUploadControlImage.HasFile)
                {
                    if (String.IsNullOrEmpty(advertise.IMAGE_DIRECTORY))//neu da co file cũ thì xóa đi để giảm dung lượng
                    {
                        string strFile = Server.MapPath(advertise.IMAGE_DIRECTORY);
                        if (File.Exists(strFile))
                        {
                            File.Delete(strFile);
                        }
                    }
                    string[] extension = ASPxUploadControlImage.FileName.Split('.');
                    if (Request.Params["status"] != null)
                    {
                        strStatus = Request.Params["status"].ToString();
                        if (Request.Params["status"] == "edit")
                        {
                            strFileName = advertise.ID.ToString() + "." + extension[1];
                        }
                        else
                        {
                            strFileName = bll_Advertise.Get_Advertise_MaxID().ToString() + "." + extension[1];
                        }
                    }

                    advertise.IMAGE_DIRECTORY = Utils.upload_directory_advertise + strFileName;                    
                    ASPxUploadControlImage.SaveAs(Server.MapPath(advertise.IMAGE_DIRECTORY), true);
                }
            }
            catch
            {
                Response.Write("<script>alert('Kiểm tra lại File Upload')</script>");
                return;
            }
            advertise.WEB_LINK = ASPxTextBoxWebLink.Text;
            if (bll_Advertise.InsertUpdate_Advertise(advertise))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Thành công');location.href = 'Admin.aspx?id=advertise';", true);
            }
        }

        protected void ASPxButtonCancel_Click(object sender, EventArgs e)
        {
            Session[Utils.SESSION_PRODUCT] = null;
            Response.Redirect(UtilsURL.advertise);
        }

        protected void ASPxButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.advertise);
        }
    }
}